
from sqlalchemy import Column, Integer, DateTime, Float, String,create_engine

from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
from sqlalchemy import create_engine

Base = declarative_base()

# 创建基类
Base = declarative_base()

# 在模块级别创建 engine（关键修改点）


# 改成 MySQL 连接字符串，替换成你的实际 MySQL 用户名、密码、数据库名
engine = create_engine('mysql+pymysql://root:123456@localhost/blood_sugar?charset=utf8mb4')


# 定义血糖数据表实体类
class BloodSugar(Base):
    """血糖数据实体类"""
    __tablename__ = 'blood_sugar'

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(String(50), nullable=False)  # 修复：添加 String 类型导入
    time = Column(DateTime, nullable=False, default=datetime.utcnow)
    amount = Column(Float, nullable=False)
    calorie = Column(Float)
    create_time = Column(DateTime, default=datetime.utcnow)  # 新增：创建时间字段
    def __repr__(self):
        return f"<BloodSugar(id={self.id}, time='{self.time}', amount={self.amount}, calorie={self.calorie})>"


# 定义加速度数据表实体类
class AccelerationData(Base):
    """加速度数据实体类"""
    __tablename__ = 'acc'

    id = Column(Integer, primary_key=True, autoincrement=True)
    datetime = Column(DateTime, nullable=False, default=datetime.utcnow)  # 添加非空约束和默认值
    acc_x = Column(Float, nullable=False)  # 添加非空约束
    acc_y = Column(Float, nullable=False)  # 添加非空约束
    acc_z = Column(Float, nullable=False)  # 添加非空约束

    def __repr__(self):
        return f"<AccelerationData(id={self.id}, datetime='{self.datetime}', acc_x={self.acc_x}, acc_y={self.acc_y}, acc_z={self.acc_z})>"


# 示例：创建数据库表（仅在直接运行此文件时执行）
if __name__ == "__main__":
    Base.metadata.create_all(engine)